package com.sprouting.ops.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprouting.ops.annotations.invoke.IdempotentSingle;
import com.sprouting.ops.interfaces.AddGroup;
import com.sprouting.ops.interfaces.ConstantBase;
import com.sprouting.ops.interfaces.UpdGroup;
import com.sprouting.ops.model.OpsSiteFault;
import com.sprouting.ops.model.OpsUser;
import com.sprouting.ops.model.ResultJson;
import com.sprouting.ops.model.qo.OpsSiteFaultQo;
import com.sprouting.ops.service.OpsSiteFaultService;
import com.sprouting.ops.utils.CheckTool;
import com.sprouting.ops.utils.EntityTool;
import com.sprouting.ops.utils.IdTool;
import com.sprouting.ops.utils.UserTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * (现场故障上报)表控制层
 *
 * @author longx
 * @since 2023-11-20 17:56:36
 */
@Slf4j
@Deprecated
@RestController
@RequestMapping("opsSiteFault")
public class OpsSiteFaultController {

    @Autowired
    private OpsSiteFaultService opsSiteFaultService;

    /**
     * 逻辑删除
     * @param opsSiteFaultId 主键
     * @return
     */
    @IdempotentSingle
    @PostMapping("delOpsSiteFault")
    public ResultJson delOpsSiteFault(String opsSiteFaultId){
        if (CheckTool.checkNull(opsSiteFaultId)){
            return ResultJson.errorCheck();
        }

        OpsSiteFault opsSiteFault = opsSiteFaultService.getById(opsSiteFaultId);
        if (opsSiteFault == null){
            return ResultJson.success();
        }

        // 逻辑删除
        opsSiteFaultService.removeById(opsSiteFault);
        return ResultJson.success();
    }

    /**
     * 分页查询
     * @param query 查询参数
     * @return
     */
    @RequestMapping("getOpsSiteFaultPageList")
    public ResultJson getOpsSiteFaultPageList(OpsSiteFaultQo query){
        if (query == null){
            query = new OpsSiteFaultQo();
        }
        Page page = query.getPage();
        return ResultJson.success(opsSiteFaultService.getOpsSiteFaultPageList(page, query));
    }

    /**
     * 保存
     * @param opsSiteFault 保存对象
     * @return
     */
    @IdempotentSingle
    @PostMapping("saveOpsSiteFault")
    public ResultJson saveOpsSiteFault(@Validated(AddGroup.class) OpsSiteFault opsSiteFault){
        String msg = checkOpsSiteFault(opsSiteFault);
        if (msg != null){
            return ResultJson.error(msg);
        }

        opsSiteFault.setId(IdTool.getId());
        opsSiteFault.setUserId(StpUtil.getLoginIdAsString());
        OpsUser user = UserTool.getLoginUser();
        opsSiteFault.setUserName(user.getName());
        opsSiteFault.setSolutionStatus(ConstantBase.SolutionStatus.REPORTED);
        opsSiteFault.setStatus(ConstantBase.SolutionStatus.EXIST);

        EntityTool.saveBase(opsSiteFault);
        opsSiteFaultService.save(opsSiteFault);
        return ResultJson.success(opsSiteFault);
    }

    /**
     * 更新
     * @param opsSiteFault 更新对象
     * @return
     */
    @IdempotentSingle
    @PostMapping("updOpsSiteFault")
    public ResultJson updOpsSiteFault(@Validated(UpdGroup.class) OpsSiteFault opsSiteFault){
        String msg = checkOpsSiteFault(opsSiteFault);
        if (msg != null){
            return ResultJson.error(msg);
        }
        if (opsSiteFault.getDiscoverTime() == null){
            opsSiteFault.setDiscoverTime(new Date());
        }
        if (opsSiteFault.getReportingTime() == null){
            opsSiteFault.setReportingTime(new Date());
        }

        EntityTool.updBase(opsSiteFault);
        opsSiteFaultService.updateById(opsSiteFault);
        return ResultJson.success(opsSiteFault);
    }

    /**
     * 检查
     * @param opsSiteFault 校验对象
     * @return 非null则有异常
     */
    private String checkOpsSiteFault(OpsSiteFault opsSiteFault){
        if (opsSiteFault.getLevel() == null){
            opsSiteFault.setLevel(ConstantBase.FaultLevel.ORDINARY);
        }


        return null;
    }
}

